/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.battleship.servlet;

import com.battleship.servlet.models.PlayerData;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author jmunoz
 */
@WebServlet(name = "PlayersInfoServlet", urlPatterns = {"/PlayersInfoServlet"})
public class PlayersInfoServlet extends HttpServlet {
    
    public static final String NAME = "namePlayer";
    public static final String ID = "idPlayer";
    public static final String BOARD_DATA = "boardData";
    
    private Vector dataVector;
    
    public PlayersInfoServlet(){
        dataVector = new Vector();
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<String> devices = Datastore.getDevices();
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            out.println("<html>");
            out.println("<head>");
            out.println("<meta http-equiv=\"refresh\" content=\"5\" >");
            out.println("<title>Servlet PlayersInfoServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Players Info !!!</h1>");
            out.println("<h2>Players connected: " + devices.size() + " </h2>");
            if (dataVector.size() != 0) {
                for (int i = 0; i < dataVector.size(); i++) {
                    PlayerData p = (PlayerData) dataVector.elementAt(i);
                    out.println("<h3>Player " + i + " name: " + p.getNamePlayer() + " </h3>");
                }

            }
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        System.out.println("GET!!");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        System.out.println("POST!!! ");
        List<String> devices = Datastore.getDevices();
        System.out.println("devices " + devices.size());
        PlayerData playerData = new PlayerData(request.getParameter("name"), devices.get(devices.size() - 1));
        dataVector.addElement(playerData);
        System.out.println("VECTOR SIZE : " +dataVector.size());
        String redirect =
                response.encodeRedirectURL(request.getContextPath() + "/jsps/Uploadtest.jsp");
        response.sendRedirect(redirect);

    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
